package com.pai.service.Role;


import com.pai.dao.BaseDao;
import com.pai.dao.role.RoleDao;
import com.pai.dao.role.RoleDaoImpl;
import com.pai.entity.AppRole;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class RoleServiceImpl implements RoleService {
    private RoleDao roleDao;  //角色dao

    public RoleServiceImpl() {
        this.roleDao = new RoleDaoImpl();
    }

    /**
     * 获得角色列表
     * @return
     */
    @Override
    public List<AppRole> getList() {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<AppRole> roleList = new ArrayList<>();
        try {
            roleList = roleDao.getList(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return roleList;
    }

    /**
     * 获取角色项详情
     * @param id
     * @return
     */
    @Override
    public AppRole getItemDetail(String id) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        AppRole roleItem = null;
        try {
            roleItem = roleDao.getItemDetail(connection, id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return roleItem;
    }

    /**
     * 根据角色名获取角色详情
     * @param roleName  角色名
     * @return
     * @throws Exception
     */
    @Override
    public AppRole getItemByName(String roleName)  {
        Connection connection = null;
        connection = BaseDao.getConnection();
        AppRole roleItem = null;
        try {
            roleItem = roleDao.getItemByName(connection, roleName);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return roleItem;
    }

    /**
     * 新增角色
     * @param role
     * @return
     */
    @Override
    public boolean add(AppRole role) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;

        try {
            flag = roleDao.add(connection, role) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return flag;
    }



    /**
     * 更新角色
     * @param role
     * @return
     */
    @Override
    public boolean update(AppRole role) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;

        try {
            flag = roleDao.update(connection, role) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return flag;
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @Override
    public boolean delete(String id) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;

        try {
            flag = roleDao.delete(connection, id) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return flag;
    }
}
